package cool.lichuan.project.system.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cool.lichuan.common.domain.Result;
import cool.lichuan.common.util.TreeBuild;
import cool.lichuan.project.system.domain.Menu;
import cool.lichuan.project.system.service.MenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lichuan
 * @date 2021/11/20 15:17
 * @description
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/menu")
public class MenuController {
    private final MenuService menuService;

    /**
     * 获取菜单列表
     *
     * @param menu 菜单信息
     * @return 返回分页
     */
    @GetMapping("/list")
    public Result<IPage<Menu>> list(Menu menu) {
        Page<Menu> menuPage = new Page<>(1, 10);
        IPage<Menu> page = menuService.page(menuPage);
        return Result.success(page);
    }
    @GetMapping("/tree")
    public Result<List<Tree<String>>> tree() {
        List<Menu> menuList = menuService.list();
        List<Tree<String>> treeList = TreeBuild.build("menuId", "menuName", menuList, new String[]{"menuUrl"});
        return Result.success(treeList);
    }
}
